#ifndef __M_BENCH_H__
#define __M_BENCH_H__
#include "./logs/log.h"
#include <chrono>

namespace log {
void bench(const std::string &loger_name, size_t thread_num, size_t msglen, size_t msg_count)
{
    Logger::ptr lp = getLogger(loger_name);
    if (lp.get() == nullptr) return;
    std::string msg(msglen, '1');
    size_t msg_count_per_thread = msg_count / thread_num;
    std::vector<double> cost_time(thread_num);
    std::vector<std::thread> threads;
    std::cout << "输入线程数量: " << thread_num << std::endl;
    std::cout << "输出日志数量: " << msg_count << std::endl;
    std::cout << "输出日志大小: " << msglen * msg_count / 1024  << "KB" << std::endl;
    for (int i = 0; i < thread_num; i++) {
        threads.emplace_back([&, i](){
            auto start = std::chrono::high_resolution_clock::now();
            for(size_t j = 0; j < msg_count_per_thread; j++) {
                lp->fatal("%s", msg.c_str());
            }
            auto end = std::chrono::high_resolution_clock::now();
            auto cost = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
            cost_time[i] = cost.count();
            auto avg = msg_count_per_thread / cost_time[i];
            std::cout << "线程" << i << "耗时: " << cost.count() << "s" << " 平均：" << (size_t)avg << "/s\n";
        });
    }
    for(auto &thr : threads) {
        thr.join();
    }
    double max_cost = 0;
    for (auto cost : cost_time) max_cost = max_cost < cost ? cost : max_cost;
    std::cout << "总消耗时间: " << max_cost << std::endl;
    std::cout << "平均每秒输出: " << (size_t)(msg_count / max_cost) << "条日志" << std::endl;
    std::cout << "平均每秒输出: " << (size_t)(msglen * msg_count / max_cost / 1024 / 1024) << "MB" << std::endl;
}
}







int main()
{
    bitlog::LogMsg msg(bitlog::LogLevel::value::INFO,53,"main.c","root","格式化功能测试...");
    bitlog::Formatter fmt;
    std::string str=fmt.format(msg);   
    bitlog::LogSink::ptr stdout_lsp =bitlog::SinkFactory::create<bitlog::StdoutSink>();
    bitlog::LogSink::ptr file_lsp =bitlog::SinkFactory::create<bitlog::FileSink>("./logfile/test.log");
    bitlog::LogSink::ptr roll_lsp =bitlog::SinkFactory::create<bitlog::RollRySizeSink>("./logfile/roll-",1024*1024);
    stdout_lsp->log(str.c_str(),str.size());
    file_lsp->log(str.c_str(),str.size());
    size_t cursize=0;
    size_t count=0;
    while (cursize<1024*1024*10)
    {
        std::string tmp=str+std::to_string(count++);
        roll_lsp->log(tmp.c_str(),tmp.size());
        cursize+=tmp.size();
    }
    
    //std::cout<<str<<std::endl<ddcd::flush;
    return 0;
}

#endif